package com.zywl.web.controller.sys;

import com.zywl.core.beans.Unit;
import com.zywl.service.UnitService;
import com.zywl.utils.models.AjaxResult;
import com.zywl.utils.models.PageData;
import com.zywl.utils.models.PageResult;
import com.zywl.utils.utils.GenerateCodeUtil;
import com.zywl.utils.utils.MapUtil;
import com.zywl.utils.utils.enums.LogTypeEnum;
import com.zywl.web.controller.base.BaseController;
import com.zywl.web.controller.form.UnitForm;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/unit")
public class UnitController extends BaseController {
    @Autowired
    private UnitService iUnitService;

    @PostMapping("/getUnitList")
    public PageResult getUnitList(HttpServletRequest request, PageData pageData) {
        PageResult pageResult = null;
        try {
            pageResult = putToResult(iUnitService.getPageInfo(MapUtil.changeMap(request.getParameterMap()), pageData));
        } catch (Exception e) {
            logger.error("获取单位列表异常", e);
        }
        return pageResult;
    }

    @GetMapping("/unitSubmitPage")
    public ModelAndView unitSubmitPage(String id) {
        ModelAndView mav = new ModelAndView("unit/unitSubmitPage");
        try {
            if (StringUtils.isNotEmpty(id)) {
                mav.addObject("unit", iUnitService.getById(Long.parseLong(id)));
            }
        } catch (Exception e) {
            logger.error("绑定参数异常", e);
        }
        return mav;
    }

    @PostMapping("/unitSubmit")
    public AjaxResult unitSubmit(HttpServletRequest request, UnitForm form) {
        AjaxResult ar = new AjaxResult();
        if (form != null) {
            String id = form.getId();
            Unit unit = null;
            String old = null;
            try {
                if (StringUtils.isNotEmpty(id)) {
                    unit = iUnitService.getById(Long.parseLong(id));
                    old = unit.getUnitName();
                } else {
                    unit = new Unit();
                    form.setId(GenerateCodeUtil.getUUIDString());
                }
                BeanUtils.copyProperties(form, unit);

                if (StringUtils.isEmpty(id)) {
                    iUnitService.save(unit);
                } else {
                    iUnitService.update(unit);
                }
            } catch (Exception e) {
                logger.error("提交单位信息异常", e);
                ar.error("提交单位信息异常", e);
                return ar;
            }

            String unitName = form.getUnitName();
            if (StringUtils.isEmpty(id)) {
                saveLog(request, LogTypeEnum.UNIT.getKey(), "单位新增-" + unitName);
                ar.success("新增成功");
            } else if (old != null && !old.equals(unitName)) {
                saveLog(request, LogTypeEnum.UNIT.getKey(), "单位修改-" + old + "→" + unitName);
                ar.success("修改成功");
            } else {
                saveLog(request, LogTypeEnum.UNIT.getKey(), "单位修改-" + unitName);
                ar.success("修改成功");
            }
        } else {
            ar.error("提交失败，参数为空");
        }
        return ar;
    }

    @PostMapping("/unitDelete")
    public AjaxResult unitDelete(String id, HttpServletRequest request) {
        AjaxResult ar = new AjaxResult();
        try {
            Unit unit = iUnitService.getById(Long.parseLong(id));
            if (unit != null) {
                String unitName = unit.getUnitName();
                iUnitService.deleteById(Long.parseLong(id));
                ar.success("删除单位成功");

                saveLog(request, LogTypeEnum.UNIT.getKey(), "单位删除-" + unitName);
            } else {
                ar.error("删除单位失败，无该单位相关信息，请刷新查看");
            }
        } catch (Exception e) {
            ar.error("删除单位异常", e);
            logger.error("删除单位异常", e);
        }
        return ar;
    }
}
